import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: 12629
 * Date: 2021/11/15
 * Time: 8:46
 * Description:
 */
public class TestDemo {


    public static int  func1(int[] array) {
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < array.length; i++) {
            if(!set.contains(array[i])) {
                set.add(array[i]);
            }else {
                return array[i];
            }
        }
        return -1;
    }

    public static HashSet<Integer> func2(int[] array) {
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < array.length; i++) {
            set.add(array[i]);
        }
        return set;
    }

    public static HashMap<Integer,Integer> func3(int[] array) {
        HashMap<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < array.length; i++) {
            int key = array[i];
            if(map.get(key) == null) {
                map.put(key,1);
            }else {
                int val = map.get(key);
                map.put(key,val+1);
            }
        }
        return map;
    }
    /*public Node copyRandomList(Node head) {
        HashMap<Node,Node> map = new HashMap<>();
        Node cur = head;
        while(cur != null) {
            Node node = new Node(cur.val);
            map.put(cur,node);
            cur = cur.next;
        }
        cur = head;
        while(cur != null) {
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        return map.get(head);
    }*/
    public int numJewelsInStones(String jewels, String stones) {
        int count = 0;
        HashSet<Character> set = new HashSet<>();
        for(char ch : jewels.toCharArray()) {
            set.add(ch);
        }

        for(char ch : stones.toCharArray()) {
            if(set.contains(ch)) {
                count++;
            }
        }
        return count;
    }

    public static void func4(String str1,String str2) {

        HashSet<Character> set = new HashSet<>();
        for(char ch : str2.toUpperCase().toCharArray()) {
            set.add(ch);//把第二个字符串中的每个字，变成大写
        }
        HashSet<Character> setBroken = new HashSet<>();//把打印过的数据  存储一份
        for(char ch : str1.toUpperCase().toCharArray()) {
            if(!set.contains(ch) && !setBroken.contains(ch)) {
                setBroken.add(ch);
                System.out.print(ch);
            }
        }
    }

    public List<String> topKFrequent(String[] words, int k) {
        HashMap<String,Integer> map = new HashMap<>();
        //1、统计单词出现的次数
        for (String s : words) {
            if(map.get(s) == null) {
                map.put(s,1);
            }else {
                Integer val = map.get(s);
                map.put(s,val+1);
            }
        }
        //2、此时已经统计好了每个单词出现的次数了,接下来，需要建立一个大小为K的小根堆
        PriorityQueue<Map.Entry<String,Integer>> minHeap =
                new PriorityQueue<>(k,new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                if(o1.getValue().compareTo(o2.getValue()) == 0) {
                    return o2.getKey().compareTo(o1.getKey());
                }
                return o1.getValue()-o2.getValue();
            }
        });

        for (Map.Entry<String,Integer> entry : map.entrySet()) {
            if(minHeap.size() < k) {
                minHeap.offer(entry);
            }else {
                Map.Entry<String,Integer> top = minHeap.peek();
                //频率相同，看字母，字母顺序小的入队
                if(top.getValue().compareTo(entry.getValue()) == 0) {
                    if(top.getKey().compareTo(entry.getKey()) > 0) {
                        minHeap.poll();
                        minHeap.offer(entry);
                    }
                }else {
                    if(top.getValue().compareTo(entry.getValue()) < 0) {
                        minHeap.poll();
                        minHeap.offer(entry);
                    }
                }
            }
        }
        List<String> ret = new ArrayList<>();
        for (int i = 0; i < k; i++) {
            Map.Entry<String,Integer> top = minHeap.poll();
            ret.add(top.getKey());
        }
        Collections.reverse(ret);
        return ret;
    }

    public static void main(String[] args) {
        String str1 = "7 This is a test";
        String str2 = " hs s a es";
        func4(str1,str2);
    }
    public static void main4(String[] args) {
        int[] array = new int[1_000];
        Random random = new Random();
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(500);
        }
        HashMap<Integer,Integer> map = func3(array);
        for(Map.Entry<Integer,Integer> entry :map.entrySet()) {
            System.out.println("数字："+entry.getKey()+" 次数："+entry.getValue());
        }

       /* HashSet<Integer> set = func2(array);
        System.out.println(set);*/
        /*int ret = func1(array);
        if(ret == -1) {
            System.out.println("没有重复的数据！");
        }else {
            System.out.println("第一个重复的数据是："+ret);
        }*/
    }
    public static void main2(String[] args) {
        //存放不重复的元素的集合
        HashSet<String> set = new HashSet<>();
        set.add("zhangsan");
        set.add("bit");
        set.add("lisi");
        set.add(null);
        //作业：搜集一下有关迭代器面试问题
        Iterator<String> it = set.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
        System.out.println("=============");
        //set.add("lisi");
        System.out.println(set);
        System.out.println(set.size());
    }

    public static void main1(String[] args) {
        HashMap<String,String> map = new HashMap<>();
        map.put("悟净","沙僧");
        map.put("及时雨","宋江");
        map.put(null,"宋江");
        map.put(null,null);
        Set<Map.Entry<String,String>> entrySet = map.entrySet();
        for (Map.Entry<String,String>  entry : entrySet) {
            System.out.println("key:"+entry.getKey()+" val: "+entry.getValue());
        }

        System.out.println("============================");
        Collection<String> list = map.values();
        System.out.println(list);
        System.out.println("============================");
        Set<String> set = map.keySet();
        System.out.println(set);
        System.out.println("============================");
        System.out.println("打印map:"+map);
        String value = map.get("及时雨");
        System.out.println(value);
        value = map.getOrDefault("及时雨2","高博");
        System.out.println(value);
        value = map.remove("悟净");
        System.out.println(value);
    }
}
